package com.aliyun.messagemgt.application.service.sms.impl;

import com.aliyun.messagemgt.application.service.sms.SmsQuotaConfigService;
import com.aliyun.messagemgt.dto.SmsQuotaDefaultConfig;
import com.aliyun.messagemgt.dto.vo.SmsQuotaRequestVO;
import com.aliyun.messagemgt.domain.repository.SmsQuotaConfigRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author byp
 * @date 2020/7/15
 */
@Service
public class SmsQuotaConfigServiceImpl implements SmsQuotaConfigService {

    @Autowired
    private SmsQuotaConfigRepository smsQuotaConfigRepository;

    private final Logger logger = LoggerFactory.getLogger(SmsQuotaConfigServiceImpl.class);

    @Override
    public int smsQuotaConfigAdd(SmsQuotaRequestVO smsQuotaRequestVO) {

        SmsQuotaDefaultConfig exist = smsQuotaConfigRepository.queryConfig(new SmsQuotaDefaultConfig());
        if (exist == null) {
            SmsQuotaDefaultConfig config = new SmsQuotaDefaultConfig();
            config.setQuota(smsQuotaRequestVO.getQuota());
            config.setQuotaType(smsQuotaRequestVO.getQuotaType());
            config.setStatus(1);
            config.setCreateTime(new Date());
            config.setCreateUserName(smsQuotaRequestVO.getUserName());
            config.setCreateUserCode(smsQuotaRequestVO.getUserId());
            return smsQuotaConfigRepository.insert(config);
        } else {
            exist.setQuota(smsQuotaRequestVO.getQuota());
            exist.setQuotaType(smsQuotaRequestVO.getQuotaType());
            exist.setUpdateUserName(smsQuotaRequestVO.getUserName());
            exist.setUpdateUserCode(smsQuotaRequestVO.getUserId());
            return smsQuotaConfigRepository.update(exist);
        }
    }

    @Override
    public SmsQuotaDefaultConfig smsQuotaConfigHistory() {
        return smsQuotaConfigRepository.queryConfig(new SmsQuotaDefaultConfig());
    }
}
